package vip.xiaonuo.facedoor.modular.listener;

import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.listener.DeviceChangeTypeEnum;
import vip.xiaonuo.facedoor.modular.device.listener.DeviceSourceEnum;
import vip.xiaonuo.facedoor.modular.device.service.DeviceSync;

import javax.annotation.Resource;

@Service
public class FacedoorChangeHandler {

    @Resource
    private FacedoorDeviceSyncService facedoorDeviceSyncService;

    public void handle(DeviceChangeTypeEnum changeType, DeviceSourceEnum source, Object obj){
        DeviceSync sync = null;
        // 先根据source获取处理对象,依赖问题，megvii module下的设备修改，自行处理同步修改
        if(source == DeviceSourceEnum.BASE){
            sync = facedoorDeviceSyncService;
        }

        if(sync!=null){
            if(changeType== DeviceChangeTypeEnum.ADD){
                Long id = sync.add(obj);
                sync.saveRelate(id,obj);
            }else if(changeType== DeviceChangeTypeEnum.EDIT){
                Long id = sync.update(obj);
                sync.saveRelate(id,obj);
            }else if(changeType== DeviceChangeTypeEnum.DELETE){
                sync.delete(obj);
            }
        }
    }


}
